#!/bin/bash

BASE_PATH="$HOME/.mara"

# registry path
REGISTRY="svr01:5000"

gitea_ip="192.168.2.102"

# TIME_ZONE
TIME_ZONE="Asia/Shanghai"

# NETWORK_NAME
NETWORK_NAME="mara-network"

SERVICE_NAME="datahub"
BUILD_DATE=`date -u +'%Y-%m-%dT%H:%M:%SZ'`
SERVICE_VERSION="current-SNAPSHOT"
DEFAULT_PORT=10003

getSecurity() {
    cat $BASE_PATH/security.json | jq -r "$1"
}

gen_docker_opts() {
  if [ -f $BASE_PATH/security.json ] ; then
    mysql_user_password=$(getSecurity ".mysql.user")
  fi

  docker_opts="\
    --restart=on-failure:5 \
    --network $NETWORK_NAME \
    ${TIME_ZONE:+ -e TZ=$TIME_ZONE} \
    ${REDIS_HOST:+ -e REDIS_HOST=$REDIS_HOST} \
    ${MYSQL_HOST:+ -e MYSQL_HOST=$MYSQL_HOST} \
    ${mysql_user_password:+ -e MYSQL_PASSWORD=$mysql_user_password}
"
}

mprint() {
  echo -e $(TZ=UTC-8 date +'%Y-%m-%dT%H:%M:%SZ') $*
}

clean_up() {
  mprint "clean up"
  if [[ $(docker images -q -f dangling=true | wc -l) -gt 0 ]]; then
    docker rmi $(docker images -q -f dangling=true)
  fi
}

build_repo() {
  COMMIT_ID=$(git rev-parse --short HEAD)
  docker build \
    --build-arg BUILD_DATE=$BUILD_DATE \
    --build-arg VCS_REF=$COMMIT_ID \
    --build-arg BUILD_VERSION=$SERVICE_VERSION \
    --tag=$REGISTRY/$SERVICE_NAME:$SERVICE_VERSION \
    --add-host gitea.in:$gitea_ip \
    .
  stop_repo
  clean_up
}

build_multi_repo() {
  [ $1 ] && SERVICE_VERSION=$1

  COMMIT_ID=$(git rev-parse --short HEAD)
  docker buildx \
    build --platform=linux/arm64,linux/amd64 \
    --build-arg BUILD_DATE=$BUILD_DATE \
    --build-arg VCS_REF=$COMMIT_ID \
    --build-arg BUILD_VERSION=$SERVICE_VERSION \
    --tag=$REGISTRY/$SERVICE_NAME:$SERVICE_VERSION \
    --add-host gitea.in:$gitea_ip \
    --push .
  # stop_repo
  # clean_up
}


clean_repo() {
  stop_repo
  if [ ! -z `docker images -q $REGISTRY/$SERVICE_NAME:$SERVICE_VERSION` ]; then
    docker rmi $REGISTRY/$SERVICE_NAME:$SERVICE_VERSION
  fi
}

push_repo() {
  docker push $REGISTRY/$SERVICE_NAME:$SERVICE_VERSION
}

stop_repo() {
  for id in `docker ps -q -a --filter name=$SERVICE_NAME`; do
    docker rm -f $id
  done
}

start_repo() {
  PORT=$DEFAULT_PORT
  [ $1 ] && PORT=$1

  gen_docker_opts

  docker run -p $PORT:$PORT -d \
    --hostname $SERVICE_NAME \
    --name $SERVICE_NAME \
    ${docker_opts:+ $docker_opts} \
    $REGISTRY/$SERVICE_NAME:$SERVICE_VERSION
}

upgrade_repo() {
  [ $1 ] && SERVICE_VERSION=$1
  stop_repo
  clean_repo
  start_repo
}

show_version() {
  echo -n $SERVICE_VERSION
}

######################
#
# main process
#
######################

usage() {
  echo "
Usage: $CMD COMMAND ...

Devops control commands.

Commands:
  build          build Docker Images from sources.
  clean          remove Docker Images in local.
  push           push Docker Images to Registry.
  start          start Docker Containers.
  stop           stop Docker Containers.

Run '$CMD COMMAND --help' for more information on a command.
"
}

CMD=$0

if [ $# == 0 ]; then
  usage
  exit 0
fi

case "$1" in
build)
  shift
  build_repo
  ;;
build-multi)
  shift
  build_multi_repo $@
  ;;
clean)
  shift
  clean_repo
  ;;  
push)
  shift
  push_repo
  ;;
start)
  shift
  start_repo $@
  ;;
stop)
  shift
  stop_repo
  ;;
upgrade)
  shift
  upgrade_repo $@
  ;;
version)
  shift
  show_version
  ;;
*)
  echo "invalid command $1"
  usage
  exit -1
  ;;
esac
